<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
    -->
    <b-modal ref="my-modal" :title="title" scrollable content-class="fullWinBody" @hidden="onHidden" size="fullWin"
        hide-footer no-close-on-backdrop>
        <div class="d-block text-center">
            <div class="card card-custom gutter-b example example-compact" id="tableVideoGroupRightBody">
                <div class="card-header">
                    <div class="card-title">
                        <span class="card-icon">
                            <i class="text-dark-50 flaticon-search-magnifier-interface-symbol"></i>
                        </span>
                        <h3 class="card-label"> 查询区域 </h3>
                    </div>
                    <div class="card-toolbar">
                        <div class="example-tools justify-content-center">
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <!--查询条件-->
                    <div class="m-form m-form--fit m-form--label-align-left m-form--group-seperator-dashed">
                        <div class="m-portlet__body">
                            <div class="form-group m-form__group row">
                                <label class="col-form-label">名称:</label>
                                <div class="col-md-2">
                                    <input type="text" class="form-control" v-model="searchForm.name" placeholder="请输入">
                                </div>
                                <label class="col-form-label">设备类型：</label>
                                <div class="col-lg-2">
                                    <el-select v-model="searchForm.device_type_id" placeholder="请选择">
                                        <el-option label="请选择" value="">
                                        </el-option>
                                        <el-option v-for="item in deviceTypeList" :key="item.device_type_id"
                                            :label="item.device_type_name" :value="item.device_type_id">
                                        </el-option>
                                    </el-select>
                                </div>
                                <label class="col-form-label">位置：</label>
                                <div class="col-lg-2">
                                    <el-select v-model="searchForm.device_location_id" placeholder="请选择">
                                        <el-option label="请选择" value="">
                                        </el-option>
                                        <el-option v-for="item in locationList" :key="item.device_location_id"
                                            :label="item.location_name" :value="item.device_location_id">
                                        </el-option>
                                    </el-select>
                                </div>
                                <b-button :pressed="false" variant="btn btn-primary font-weight-bold mr-2"
                                    @click="search()"><span><i class="fa fa-search"></i><span>查询</span></span></b-button>
                                <b-button :pressed="false" variant="btn btn-light font-weight-bold mr-2"
                                    @click="resetAll()">
                                    <span><span>清空条件</span></span></b-button>
                                <button class="btn btn-light-info font-weight-bold mr-2 " @click="selectDevice">
                                    <span><i class="la la-plus la-lg"></i><span>添加摄像头</span></span>
                                </button>
                            </div>
                        </div>
                        <div class="m-portlet__foot m-portlet__no-border m-portlet__foot--fit">
                            <div class="m-form__actions m-form__actions--solid">
                                <div class="row">
                                    <div class="col m--align-left">

                                    </div>
                                    <div class="col m--align-right">

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!--分页组件-->
                <el-table :style="style" stripe border highlight-current-row
                    :data="dataList" max-height="250">
                    <!-- 复选框 -->
                    <el-table-column type="selection" width="40"></el-table-column>
                    <el-table-column label="序号" min-width="50">
                        <template slot-scope="scope">
                            {{ scope.$index + 1 }}
                        </template>
                    </el-table-column>
                    <el-table-column align="left" prop="device_type_name" show-overflow-tooltip label="设备类型" ></el-table-column>
                    <el-table-column align="left" prop="device_code" show-overflow-tooltip label="设备编号" ></el-table-column>
                    <el-table-column align="left" prop="name" show-overflow-tooltip label="名称" ></el-table-column>
                    <el-table-column align="center" prop="location_name" label="位置" min-width="150"></el-table-column>
                    <el-table-column align="center" label="操作" fixed="left">
                        <template slot-scope="scope">
                            <!--scope.row当前行的对象-->
                            <a href="javascript:;" @click="deleteVideoGroupRight(scope.row)"
                                class="btn btn-sm btn-clean btn-icon" title="详情">
                                <span class="svg-icon svg-icon-primary svg-icon-2x"><svg xmlns="http://www.w3.org/2000/svg"
                                        xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px"
                                        viewBox="0 0 24 24" version="1.1">
                                        <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
                                            <rect x="0" y="0" width="24" height="24" />
                                            <path
                                                d="M6,8 L6,20.5 C6,21.3284271 6.67157288,22 7.5,22 L16.5,22 C17.3284271,22 18,21.3284271 18,20.5 L18,8 L6,8 Z"
                                                fill="#000000" fill-rule="nonzero" />
                                            <path
                                                d="M14,4.5 L14,4 C14,3.44771525 13.5522847,3 13,3 L11,3 C10.4477153,3 10,3.44771525 10,4 L10,4.5 L5.5,4.5 C5.22385763,4.5 5,4.72385763 5,5 L5,5.5 C5,5.77614237 5.22385763,6 5.5,6 L18.5,6 C18.7761424,6 19,5.77614237 19,5.5 L19,5 C19,4.72385763 18.7761424,4.5 18.5,4.5 L14,4.5 Z"
                                                fill="#000000" opacity="0.3" />
                                        </g>
                                    </svg>
                                </span>
                            </a>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
        </template>
        <DeviceSelect ref="deviceSelectRef" :deviceOption="{ deviceOption }" @change="doSetDevice"></DeviceSelect>
        <!--采用父窗口关闭传递的方法-->
    </b-modal>
</template>
<script>
import apiUtil from "@/core/util/apiUtil.js";
import DeviceSelect from "@/view/monitor/iot-components/device-components.vue";
import { handleNotify, handleAlert, handleConfirm, showWating, closeWating } from "@/core/util/jehcUtil.js";
export default {
    data() {
        return {
            deviceOption: { allowMultiSelect: true },
            searchForm: { video_group_id: "", device_type_id: "", device_location_id: "" },
            video_group_id: "",
            locationList: [],
            deviceTypeList: [],
            style:{width:"100%"},
            title: "视频组-相机",
            dataList: [],
            sels: [], //当前选框选中的值
        }
    },
    components: {
        DeviceSelect,
    },
    watch: {
    },
    mounted() {

    },
    methods: {
        showModal(row) {
            this.title = "视频组-关联组【" + row.group_name + "】";
            this.video_group_id = row.video_group_id;
            this.$refs['my-modal'].show();
            this.resetAll();
            this.initVideoGroupRightList(row);
            this.initDeviceTypeList();
            this.initDeviceLocationList();
        },
        handleSelectionChange(sels) {//获取选中的值
            this.sels = sels;
        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        onHidden() {//关闭窗体触发事件
            this.$emit("change", null);
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        }, 
    
        search() {
            this.pageNo = 1;       // 更新当前的页
            this.initVideoGroupRightList();          // 按新的pageNo和pageSize进行查询
        },
        resetAll() {
            Object.assign(this.$data.searchForm, this.$options.data().searchForm);
            this.search();
        },
        deleteVideoGroupRight(row) {
            this.$confirm("确认删除记录吗?", "提示", {type: "warning",}).then(() => {
                var params = {video_group_right_id:row.video_group_right_id,_method:'DELETE'};
                apiUtil.delete(process.env.VUE_APP_MONITOR_API+"/iotVideoGroup/remove",params).then(data=>{
                    if(data.data.success){
                    handleAlert("删除成功", "success", 3);
                    this.search();
                    }else {
                    handleAlert("删除失败", "error", 3);
                    }
                });
                }).catch(()=>{});//注意这里，这里是重点！！！;
        },
        selectDevice() {
            this.$refs.deviceSelectRef.showModal();
        },
        initVideoGroupRightList() {
            showWating({ renderBody: "tableVideoGroupRightBody" });
            var params = {};
            params.usePageNo = true;//采用第几页进行分页（兼容）
            params.pageSize = this.pageSize;//页面显示记录条数，在页面显示每页显示多少项的时候
            params.pageNo = this.pageNo;//开始的记录序号   
            this.searchForm.video_group_id = this.video_group_id;
            params.searchJson = JSON.stringify(this.searchForm);//为form元素     
            apiUtil.queryPage(process.env.VUE_APP_MONITOR_API + "/iotVideoGroup/rightList", params).then(({ data }) => {
                this.dataList = data.data;//给结果集赋值
                this.totalCount = data.total;// 获取当前数据的总数    
            });
        },
        initDeviceTypeList() {
            let params = {};
            apiUtil.query(process.env.VUE_APP_MONITOR_API + "/iotDeviceType/allList", params).then(({ data }) => {
                this.deviceTypeList = data.data;
            });
        },
        initDeviceLocationList() {
            let params = {};
            apiUtil.query(process.env.VUE_APP_MONITOR_API + "/iotDeviceLocation/allList", params).then(({ data }) => {
                this.locationList = data.data;
            });
        },
        doSetDevice(data) {
            if (data) {
                let device_id = data.map((item) => item.device_id);
                let names = data.map((item) => item.name);
                let params = {device_id:device_id.join(","),video_group_id:this.video_group_id};
                this.$confirm("确定添加相机："+names+"?", "提示", { type: "warning", }).then(() => {
                    apiUtil.post(process.env.VUE_APP_MONITOR_API + "/iotVideoGroup/addRight", params).then(({ data }) => {
                        if (data.success) {
                            handleAlert("操作成功", "success", 3);
                            this.search();
                        } else {
                            handleAlert("操作失败", "error", 3);
                        }
                    });
                }).catch(() => { });//注意这里，这里是重点！！！;   
            }
        }
    }
}
</script>